#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class tcp_socket
{
public:
    tcp_socket() : _sock(-1) {}

    bool Available()
    {
        if (_sock < 0)
            return false;
        return true;
    }

    bool Socket()
    {
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            std::cout << "socket error" << std::endl;
            return false;
        }
        return true;
    }

    bool Bind(const std::string &ip, uint16_t port)
    {
        struct sockaddr_in peer;
        peer.sin_family = AF_INET;
        peer.sin_port = htons(port);
        peer.sin_addr.s_addr = inet_addr(ip.c_str());
        if (bind(_sock, (struct sockaddr *)&peer, sizeof(peer)) < 0)
        {
            std::cout << "bind error" << std::endl;
            return false;
        }
        return true;
    }

    bool Listen()
    {
        if (listen(_sock, 5) < 0)
        {
            std::cout << "listen error" << std::endl;
            return false;
        }
        return true;
    }
    tcp_socket Accept(std::string *peer, uint16_t *port)
    {
        struct sockaddr_in addr;
        socklen_t len = sizeof(addr);
        tcp_socket tmp;
        tmp._sock = accept(_sock, (struct sockaddr *)&addr, &len);
        if (tmp._sock < 0)
        {
            std::cout << "accept error" << std::endl;
            return tmp;
        }
        *peer = inet_ntoa(addr.sin_addr);
        *port = ntohs(addr.sin_port);
        return tmp;
    }

    bool Recv(std::string *body, size_t len = 4096)
    {
        body->resize(len);
        size_t ret = recv(_sock, &(*body)[0], len, 0);
        if (ret < 0)
        {
            std::cout << "recv error" << std::endl;
            return false;
        }
        if (ret == 0)
        {
            std::cout << "connet break" << std::endl;
            return false;
        }
        body->resize(ret);
        return true;
    }

    bool Send(const std::string &body)
    {
        int len = 0;
        while (len < body.size())
        {
            ssize_t res = send(_sock, body.c_str()+len, body.size(), 0);
            if(res<0)
            {
                std::cout<<"send error"<<std::endl;
                return false;
            }
            len+=res;
        }
        return true;
    }

    bool Connect(std::string& ip,uint16_t& port)
    {
        struct sockaddr_in serv;
        serv.sin_family=AF_INET;
        serv.sin_port=htons(port);
        serv.sin_addr.s_addr=inet_addr(ip.c_str());
        if(connect(_sock,(struct sockaddr*)&serv,sizeof(serv))<0)
        {
            std::cout<<"connect error"<<std::endl;
            return false;
        }
        return true;
    }
    bool Close()
    {
        if(_sock==-1)
        return false;
        close(_sock);
        _sock=-1;
        return true;
    }

private:
    int _sock;
};